package cn.tarena.ht.controller;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.tarena.ht.pojo.Module;
import cn.tarena.ht.pojo.Role;
import cn.tarena.ht.service.ModuleService;
import cn.tarena.ht.service.RoleService;
import cn.tarena.ht.tool.IdCheck;

@Controller
@RequestMapping("/sysadmin/role")
public class RoleController extends BaseController{
	@Autowired
	private RoleService roleService;
	@Autowired
	private ModuleService moduleService;
	
	@RequestMapping("/list")
	public String roleList(Model model) {
		List<Role> roleList = roleService.findAll();
		model.addAttribute("roleList", roleList);
		return "/sysadmin/role/jRoleList";
	}
	
	@RequestMapping("/tocreate")
	public String toCreate(Model model) {
		String roleId = UUID.randomUUID().toString();
		return "/sysadmin/role/jRoleCreate";
	}
	
	@RequestMapping("/save")
	public String saveRole(Role role) {
		role.setRoleId(UUID.randomUUID().toString());
		role.setCreateTime(new Date());
		role.setUpdateTime(role.getCreateTime());
		roleService.saveRole(role);
		return "redirect:/sysadmin/role/list";
	}
	
	@RequestMapping("/delete")
	public String deleteRole(@RequestParam(value="roleId",required=false )String[] roleIds,HttpSession session) {
		boolean result = IdCheck.checkIds(roleIds, session);
		if(result) {
			roleService.deleteRole(roleIds);
		}
		return "redirect:/sysadmin/role/list";
	}
	
	@RequestMapping("/toview")
	public String toView(@RequestParam(value="roleId",required=false)String[] roleIds,HttpSession session) {
		boolean result = IdCheck.checkId(roleIds, session);
		if(result) {
			Role role = roleService.findRole(roleIds[0]);
			session.setAttribute("role", role);
			return "/sysadmin/role/jRoleView";
		}else {
			return "redirect:/sysadmin/role/list";
		}
	}
	
	@RequestMapping("/toupdate")
	public String toUpdate(@RequestParam(value="roleId",required=false)String[] roleIds,HttpSession session) {
		boolean result = IdCheck.checkId(roleIds, session);
		if(result) {
			Role role = roleService.findRole(roleIds[0]);
			session.setAttribute("role", role);
			return "/sysadmin/role/jRoleUpdate";
		}else {
			return "redirect:/sysadmin/role/list";
		}
	}
	
	@RequestMapping("/update")
	public String updateRole(Role role) {
		role.setUpdateTime(new Date());
		roleService.updateRole(role);
		return "redirect:/sysadmin/role/list";
	}
	
	@RequestMapping("/roleModule")
	public String roleModule(@RequestParam(value="roleId",required=false)String[] roleIds,HttpSession session) throws JsonProcessingException {
		boolean result = IdCheck.checkId(roleIds, session);
		if(result) {
			List<String> rModuleList = roleService.findModules(roleIds[0]);
			List<Module> moduleList = moduleService.findAll();
			for (Module module : moduleList) {
				if(rModuleList.contains(module.getModuleId())) {
					module.setChecked(true);
				}
			}
			ObjectMapper objectMapper = new ObjectMapper();
			String zTreeJson = objectMapper.writeValueAsString(moduleList);
			session.setAttribute("zTreeJson", zTreeJson);
			session.setAttribute("roleId", roleIds[0]);
			return "/sysadmin/role/jRoleModulezTree";
		}else {
			return "redirect:/sysadmin/role/list";
		}
	}
	
	@RequestMapping("/saveRoleModule")
	public String saveRoleModule(String roleId,String[] moduleIds) {
		roleService.saveRoleModule(roleId,moduleIds);
		return "redirect:/sysadmin/role/list";
	}
}
